// 替换手机字符
export function regMobile(mobile) {
  if(!mobile) {
    return mobile
  }
  let new_mobile = ''
  if (mobile.length > 7) {
    // new_mobile = mobile.substr(0, 5) + '****' + mobile.substr(1)
    new_mobile = mobile.substr(0, 3) + '***'
  }else {
    new_mobile = mobile.substr(0, 4) + '***'
  }
  return new_mobile
}
//
export function makeExpandingArea(el) {
  var timer = null;
  //由于ie8有溢出堆栈问题，故调整了这里
  var setStyle = function(el, auto) {
    if (auto) el.style.height = 'auto';
    el.style.height = el.scrollHeight + 'px';
  }
  var delayedResize = function(el) {
    if (timer) {
      clearTimeout(timer);
      timer = null;
    }
    timer = setTimeout(function() {
      setStyle(el)
    }, 200);
  }
  if (el.addEventListener) {
    el.addEventListener('input', function() {
      setStyle(el, 1);
    }, false);
    setStyle(el)
  } else if (el.attachEvent) {
    el.attachEvent('onpropertychange', function() {
      setStyle(el)
    })
    setStyle(el)
  }
  if (window.VBArray && window.addEventListener) { //IE9
    el.attachEvent("onkeydown", function() {
      var key = window.event.keyCode;
      if (key == 8 || key == 46) delayedResize(el);

    });
    el.attachEvent("oncut", function() {
      delayedResize(el);
    }); //处理粘贴
  }
}
// 列表参数回显
export function tableDicts(state,list) {
  let lists = filterMenu (list, [])
  if(typeof state === 'number' && !isNaN(state)) {
    state = state.toString()
  }
  let obj = lists.find((item) => {
    return item.value == state
  })
  return obj ? obj.label : ''
}

function filterMenu (array, resData) {
  if (Array.isArray(array) && array.length > 0) {
    array.forEach(function(v, i) {
      resData.push(v)
      if(v.childrenList && v.childrenList.length > 0){
        filterMenu(v.childrenList, resData)
      }
    })
  }
  return resData
}
